/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary;

import entity.DaftarMember;
import entity.DaftarMemberTable;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jpa.DaftarMemberJpaController;

/**
 *
 * @author Sonnavy
 */
@WebServlet(name = "DaftarMemberServlet", urlPatterns = {"/DaftarMemberServlet"})
public class DaftarMemberServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page = request.getParameter("page");
        RequestDispatcher dis = null;

        if (page != null) {
            if (page.equals("registered")) {
                DaftarMemberServletLogic serv = new DaftarMemberServletLogic(request);
                dis = request.getRequestDispatcher(serv.signUp());
            } else if (page.equals("register")) {
                dis = request.getRequestDispatcher("index.jsp");
            } else if (page.equals("index")) {
                dis = request.getRequestDispatcher("index_1.jsp");
            }

        } else {
            dis = request.getRequestDispatcher("index_1.jsp");
        }
        dis.forward(request, response);

        String erorusername = "";
        String username = request.getParameter("username");


        if (username != null) {
            if (username.equals("")) {
                username = "Field username Tidak Boleh Kosong!!!";
            }
        }
        if (!"".equals(username)) {
            request.setAttribute("username", username);
        }
        String errorpassword = "";
        String password = request.getParameter("password");

        if (password != null) {
            if (password.equals("")) {
                password = "Field password Tidak Boleh Kosong!!!";
            }
        }
        if (!"".equals(password)) {
            request.setAttribute("password", password);
            //request.getRequestDispatcher("signUp.jsp").forward(request, response);
        }
        String erornamadepan = "";
        String nama_depan = request.getParameter("namaDepan");

        if (nama_depan != null) {
            if (nama_depan.equals("")) {
                nama_depan = "Field nama depan Tidak Boleh Kosong!!!";
            }
        }
        if (!"".equals(nama_depan)) {
            request.setAttribute("namadepan", nama_depan);
            //request.getRequestDispatcher("signUp.jsp").forward(request, response);
        }
        String erornamabelakang = "";
        String nama_belakang = request.getParameter("namaBelakang");

        if (nama_belakang != null) {
            if (nama_belakang.equals("")) {
                nama_belakang = "Field nama belakang Tidak Boleh Kosong!!!";
            }
        }
        if (!"".equals(nama_belakang)) {
            request.setAttribute("namabelakang", nama_belakang);

        }
        String errornamapanggil = "";
        String nama_panggil = request.getParameter("nama_paggil");

        if (nama_panggil != null) {
            if (nama_panggil.equals("")) {
                nama_panggil = "Field nama panggil Tidak Boleh Kosong!!!";
            }
        }
        if (!"".equals(nama_panggil)) {
            request.setAttribute("namapanggil", nama_panggil);

        }
        String errorstatus = "";
        String status = request.getParameter("status");

        if (status != null) {
            if (status.equals("")) {
                status = "Field status Tidak Boleh Kosong!!!";
            }
        }
        if (!"".equals(status)) {
            request.setAttribute("status", status);

        }
        String erroralamat = "";
        String alamat = request.getParameter("alamat");

        if (alamat != null) {
            if (alamat.equals("")) {
                alamat = "Field alamat Tidak Boleh Kosong!!!";
            }
        }
        if (!"".equals(alamat)) {
            request.setAttribute("alamat", alamat);

        }
        String erorkota = "";
        String kota = request.getParameter("kota");

        if (kota != null) {
            if (kota.equals("")) {
                kota = "Field kota Tidak Boleh Kosong!!!";
            }
        }
        if (!"".equals(kota)) {
            request.setAttribute("kota", kota);

        }
        String errorprovinsi = "";
        String provinsi = request.getParameter("provinsi");

        if (provinsi != null) {
            if (provinsi.equals("")) {
                provinsi = "Field provinsi Tidak Boleh Kosong!!!";
            }
        }
        if (!"".equals(provinsi)) {
            request.setAttribute("errorNoTelpon", provinsi);

        }
        String errorkodepos = "";
        String kodepos = request.getParameter("kodepos");

        if (kodepos != null) {
            if (kodepos.equals("")) {
                kodepos = "Field kodepos Tidak Boleh Kosong!!!";
            }
        }
        if (!"".equals(kodepos)) {
            request.setAttribute("kodepos", kodepos);
        }
        String errortelepon = "";
        String teleponHp = request.getParameter("teleponHp");

        if (teleponHp != null) {
            if (teleponHp.equals("")) {
                teleponHp = "Field telepon / HP Tidak Boleh Kosong!!!";
            }
        }
        if (!"".equals(teleponHp)) {
            request.setAttribute("teleponHp", teleponHp);
        }
        String erroremail = "";
        String email = request.getParameter("email");

        if (email != null) {
            if (email.equals("")) {
                email = "Field email Tidak Boleh Kosong!!!";
            }
        }
        if (!"".equals(email)) {
            request.setAttribute("email", email);
        }
        DaftarMember DaftarMember = new DaftarMember();
        DaftarMemberTable dm = new DaftarMemberTable();
        if (request.getParameter("nama_depan") != null) {
            DaftarMember.setNamaDepan(nama_depan);
            DaftarMember.setNamaPanggil(nama_panggil);
            DaftarMember.setStatus(status);
            DaftarMember.setAlamat(alamat);
            DaftarMember.setKota(kota);
            DaftarMember.setProvinsi(provinsi);
            DaftarMember.setKodepos(kodepos);
            DaftarMember.setTeleponHp(teleponHp);
            DaftarMember.setEmail(email);

            dm.create(DaftarMember);
            response.sendRedirect(response.encodeRedirectURL(""));
        } else {
            //request.setAttribute("errorNama", errorNama);
            request.getRequestDispatcher("halaman_register.jsp").include(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
